<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;

class InitSetup extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'init:setup';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'run db:seed->perm:gen->build:test';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $q = $this->ask('redo migrate?(y to continue)','n');
        if(strtolower($q)=='y'){
            \DB::unprepared('SET FOREIGN_KEY_CHECKS=0');
            try {
                $cnt = \DB::table('migrations')->count();
                while ($cnt){
                    $this->info("$cnt");
                    $this->call('migrate:rollback');
                    $cnt = \DB::table('migrations')->count();
                }
            } catch (\Exception $exception) {
                $this->info('error');
                $this->error($exception->getMessage());
            }
            $this->call('migrate');
        }



        $q = $this->ask('db:seed?(y to continue)','n');
        if(strtolower($q)=='y'){
            $this->call('db:seed');
        }

        $q = $this->ask('perm:gen?(y to continue)','n');
        if(strtolower($q)=='y'){
            $this->call('perm:gen');
            $this->call('create:users');
        }

        $this->call('passport:install',['-n']);

        $users = User::all()->pluck('name','id');
        print_r($users);
        $this->call('passport:client',['--client','--name'=>'c1']);
        $this->call('passport:client',['--client','--name'=>'c2']);
        $this->call('passport:client',['--client','--name'=>'c3']);
        $this->call('passport:client',['--client','--name'=>'c4']);
        $this->call('passport:client',['--client','--name'=>'c5']);


        $path = database_path('staff_dorm.csv');
        if(file_exists($path)){
            $q = $this->ask('import:csv?(y to continue)','n');
            if(strtolower($q)=='y'){
                $this->call('import:csv');
            }
        }else{
            $q = $this->ask('build:test?(y to continue)','n');
            if(strtolower($q)=='y'){
                $this->call('build:test');
            }
        }

        $this->call('build:period');

    }
}
